﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StandardConverters.cs" company="CodePlex">
//   The MIT License
//   Copyright (c) 2013 Julien Blin
//   
//   Permission is hereby granted, free of charge, to any person obtaining a copy
//   of this software and associated documentation files (the "Software"), to deal
//   in the Software without restriction, including without limitation the rights
//   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//   copies of the Software, and to permit persons to whom the Software is
//   furnished to do so, subject to the following conditions:
//   
//   The above copyright notice and this permission notice shall be included in
//   all copies or substantial portions of the Software.
//   
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//   THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace UCodeFirst
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Web;
    using System.Xml.Linq;

    using UCodeFirst.ContentTypes;
    using UCodeFirst.Converters;

    using Umbraco.Core;

    /// <summary>
    /// Holds the list of standards <see cref="IConverter"/>.
    /// </summary>
    public static class StandardConverters
    {
        /// <summary>
        /// The default converters.
        /// </summary>
        private static readonly Lazy<IDictionary<Type, IConverter>> DefaultConverters = new Lazy<IDictionary<Type, IConverter>>(InitDefaultConverters, true);

        /// <summary>
        /// The converters.
        /// </summary>
        private static IDictionary<Type, IConverter> converters;

        /// <summary>
        /// Gets or sets the standard converters.
        /// </summary>
        public static IDictionary<Type, IConverter> Converters
        {
            get
            {
                return converters ?? (converters = DefaultConverters.Value);
            }

            set
            {
                converters = value;
            }
        }

        /// <summary>
        /// Initialize the list of default converters.
        /// </summary>
        /// <returns>
        /// The initialized list.
        /// </returns>
        private static IDictionary<Type, IConverter> InitDefaultConverters()
        {
            var result = new Dictionary<Type, IConverter>
                             {
                                 { typeof(string), new StringConverter() },
                                 { typeof(IHtmlString), new HtmlStringConverter() },
                                 { typeof(HtmlString), new HtmlStringConverter() },
                                 { typeof(int), new Int32Converter() },
                                 { typeof(int?), new Int32Converter() },
                                 { typeof(double), new DoubleConverter() },
                                 { typeof(double?), new DoubleConverter() },
                                 { typeof(decimal), new DecimalConverter() },
                                 { typeof(decimal?), new DecimalConverter() },
                                 { typeof(bool), new BooleanConverter() },
                                 { typeof(bool?), new BooleanConverter() },
                                 { typeof(DateTime), new DateTimeConverter() },
                                 { typeof(DateTime?), new DateTimeConverter() },
                                 { typeof(Color), new Converters.ColorConverter() },
                                 { typeof(Color?), new Converters.ColorConverter() },
                                 { typeof(XDocument), new XDocumentConverter() }
                             };

            // Specializing EnumConverter.
            AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes().Where(t => t.IsEnum && !t.IsGenericType)).ForEach(x => result.Add(x, new EnumConverter(x)));

            // Real proxies
            var realProxyConverter = new RealProxyConverter();
            var csvProxyConverter = new CsvContentIdProxyConverter();
            TypesScanner.GetClassesInBinThatAreAssignableFrom<ContentTypeBase>().ForEach(x =>
                {
                    result.Add(x, realProxyConverter);
                    result.Add(typeof(IEnumerable<>).MakeGenericType(x), csvProxyConverter);
                    result.Add(Array.CreateInstance(x, 0).GetType(), csvProxyConverter);
                });
            return result;
        }
    }
}
